package xgd

import (
	D "TCP/define"
	"github.com/gin-gonic/gin"
	"net/http"
	"regexp"
	"sort"
	"strconv"
	"time"
	"unicode/utf8"
	"xlib/log"
)

// user_name 长度不超过32个字节或者10个汉字
func ValidateUserName(c *gin.Context, userName string) bool {
	if len(userName) == 0 {
		D.ErrorCodeWithMsg(c, http.StatusOK, D.EcParamMissing, D.EmParamMissing)
		return false
	}
	if len(userName) > 32 && utf8.RuneCountInString(userName) > 10 {
		D.ErrorCodeWithMsg(c, http.StatusOK, D.EcsParamExceed, D.EmParamExceed)
		return false
	}
	return true
}

// card_no 最大长度10
func ValidateCardNo(c *gin.Context, cardNo string) bool {
	if len(cardNo) == 0 {
		D.ErrorCodeWithMsg(c, http.StatusOK, D.EcParamMissing, D.EmParamMissing)
		return false
	}
	if len(cardNo) > 10 {
		D.ErrorCodeWithMsg(c, http.StatusOK, D.EcsParamExceed, D.EmParamExceed)
		return false
	}
	return true
}

// cardPwd 4~8
func ValidateCardPassword(c *gin.Context, pwd string) bool {
	if len(pwd) > 8 || len(pwd) < 4 {
		D.ErrorCodeWithMsg(c, http.StatusOK, D.EcsParamExceed, D.EmParamExceed)
		return false
	}
	return true
}

type Timezone [][2]int

func (tz Timezone) Len() int {
	return len(tz)
}

func (tz Timezone) Less(i, j int) bool {
	return tz[i][0] < tz[j][0] && tz[i][1] < tz[j][1]
}

func (tz Timezone) Swap(i, j int) {
	tz[i], tz[j] = tz[j], tz[i]
}

// 时间段begin不小于end
func ValidateTimezone(c *gin.Context, timezones []D.TimeSlot) bool {
	if timezones == nil || len(timezones) == 0 {
		D.ErrorCodeWithMsg(c, http.StatusOK, D.EcParamMissing, D.EmParamMissing)
		return false
	}

	var tmp Timezone = make([][2]int, 0)
	for _, ts := range timezones {
		log.Debugf("ts", ts)
		beginTime, err := time.Parse("15:04", ts.Begin)
		if err != nil {
			log.Error("wrong begin time:", ts.Begin)
			D.ErrorCodeWithMsg(c, http.StatusOK, D.EcsParamExceed, D.EmParamExceed)
			return false
		}
		endTime, err := time.Parse("15:04", ts.End)
		if err != nil {
			log.Error("wrong end time:", ts.End)
			D.ErrorCodeWithMsg(c, http.StatusOK, D.EcsParamExceed, D.EmParamExceed)
			return false
		}
		minTs := [2]int{beginTime.Hour()*60 + beginTime.Minute(), endTime.Hour()*60 + endTime.Minute()}
		if !ValidateTime24(ts.Begin) || !ValidateTime24(ts.End) {
			D.ErrorCodeWithMsg(c, http.StatusOK, D.EcsParamExceed, D.EmParamExceed)
			return false
		}
		if minTs[0] >= minTs[1] {
			D.ErrorCodeWithMsg(c, http.StatusOK, D.EcParamFormat, D.EmParamFormat)
			return false
		}
		tmp = append(tmp, minTs)
	}
	log.Infof("timezones: %#v", tmp)

	sort.Sort(tmp)
	for i := 0; i < len(tmp)-1; i++ {
		if tmp[i][1] > tmp[i+1][0] {
			log.Error("cross timezone existed!")
			D.ErrorCodeWithMsg(c, http.StatusOK, D.EcParamFormat, D.EmParamFormat)
			return false
		}
	}
	return true
}

// doorname 不超过 64 字节 或者 31 汉字
func ValidateDoorName(c *gin.Context, doorName string) bool {
	if len(doorName) == 0 {
		D.ErrorCodeWithMsg(c, http.StatusOK, D.EcParamMissing, D.EmParamMissing)
		return false
	}
	if len(doorName) > 64 && utf8.RuneCountInString(doorName) > 21 {
		D.ErrorCodeWithMsg(c, http.StatusOK, D.EcsParamExceed, D.EmParamExceed)
		return false
	}
	return true
}

func ValidateCardFlag(c *gin.Context, cardFlag int) bool {
	if cardFlag != 0 && cardFlag != 1 {
		D.ErrorCodeWithMsg(c, http.StatusOK, D.EcsParamExceed, D.EmParamExceed)
		return false
	}
	return true
}

func ValidateGroupNo(c *gin.Context, groupNo int) bool {
	if groupNo < 0 || groupNo > 11 {
		D.ErrorCodeWithMsg(c, http.StatusOK, D.EcGroupNoNotExist, D.EmGroupNoNotExist+strconv.Itoa(groupNo))
		return false
	}
	return true
}

func ValidateWeek(c *gin.Context, week []int) bool {
	for _, ts := range week {
		if ts > 7 || ts < 1 {
			D.ErrorCodeWithMsg(c, http.StatusOK, D.EcsParamExceed, D.EmParamExceed)
			return false
		}
	}
	return true
}

// 00:00~23:59
func ValidateTime24(timeStr string) bool {
	reg, _ := regexp.Compile("^([0-1][0-9]|2[0-3]):[0-5][0-9]$")
	return reg.Match([]byte(timeStr))
}

func ValidateTime(timeStr string) bool {
	_, err := time.Parse("", timeStr)
	return err == nil
}
